<?php
/**
 * EcataloguE
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage ecatalogue
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/EcataloguE
 */

include_spip('inc/meta');
include_spip('base/create');
function ecatalogue_upgrade($nom_meta_base_version, $version_cible){
    
	$current_version = "0.0";

    // If plugin is already installed, get it's base version
	if (isset($GLOBALS['meta'][$nom_meta_base_version])) {
		$current_version = $GLOBALS['meta'][$nom_meta_base_version];
		//spip_log('12:14 Version actuelle : '.$current_version,'ecatalogue');        
	}

    // If plugin's base version is 0.0 (it is not installed) create database
	if ($current_version=="0.0") {

        // check if you already have an old database
        $r = mysql_query("SELECT `type` FROM `spip_ecatalogue_groupes` WHERE 0");
        if ($r) {
            sql_alter("TABLE spip_ecatalogue_groupes RENAME spip_ecatalogue_proprietes");
            sql_alter("TABLE spip_ecatalogue_proprietes CHANGE id_groupe id_propriete bigint(21) NOT NULL");
            sql_alter("TABLE spip_ecatalogue_options CHANGE id_groupe id_propriete bigint(21) NOT NULL");
        }

        creer_base();
        // Write new version to sql cache. 
        //$current_version became latest, therefore all next version_compare checks won't be executed
		ecrire_meta($nom_meta_base_version, $current_version=$version_cible);
		//spip_log('Base de donnйes ecatalogue correctement peuplйe','ecatalogue');
	}

    // If plugin's base version is < 0.1, alter some tables
    if (version_compare($current_version,"0.1.0","<")) {
        // Upgrade via sql_alter
        //sql_alter("TABLE spip_ecatalogue_products ADD COLUMN id_equipment bigint(21) NOT NULL");
        
        // Upgrade via maj_tables (it tells SPIP to refresh some tables)
        maj_tables('spip_ecatalogue_products');
        
        // Write new version to sql cache
        ecrire_meta($nom_meta_base_version, $current_version="0.1");
    }
    
    if (version_compare($current_version,"0.2.0","<")) {        
        //maj_tables('spip_ecatalogue_articles_similars');
        creer_base();
        maj_tables('spip_ecatalogue_products');
        ecrire_meta($nom_meta_base_version, $current_version="0.2");
    }
    
    if (version_compare($current_version,"0.3.0","<")) {        
        // Create tables that wasn't created yet
        creer_base();
        ecrire_meta($nom_meta_base_version, $current_version="0.3");
    }
    
    if (version_compare($current_version,"0.4.0","<")) {        
        // Create tables that wasn't created yet
        //creer_base();
        maj_tables('spip_ecatalogue_products');
        ecrire_meta($nom_meta_base_version, $current_version="0.4");
    }
    
    if (version_compare($current_version,"0.5.0","<")) {        
        // Create tables that wasn't created yet
        creer_base();        
        ecrire_meta($nom_meta_base_version, $current_version="0.5");
    }
    
    if (version_compare($current_version,"0.6.0","<")) {    	
    	maj_tables('spip_ecatalogue_products');
    	ecrire_meta($nom_meta_base_version, $current_version="0.6");
    }
    
    if (version_compare($current_version,"0.7.0","<")) {    	
    	maj_tables('spip_ecatalogue_composites_articles');
    	ecrire_meta($nom_meta_base_version, $current_version="0.7");
    }
    
    if (version_compare($current_version,"0.9.0","<")) {    	
    	maj_tables('spip_ecatalogue_options');
    	ecrire_meta($nom_meta_base_version, $current_version="0.9");
    }

    if (version_compare($current_version,"0.10.0","<")) {
        // Create tables that wasn't created yet (spip_ecatalogue_proprietes,  spip_ecatalogue_rubriques_groupes)
        creer_base();
        // Update tables
    	maj_tables('spip_ecatalogue_options');
        maj_tables('spip_ecatalogue_groupes');
    	ecrire_meta($nom_meta_base_version, $current_version="0.10");
    }
    
     if (version_compare($current_version,"0.11.0","<")) {
        // Create tables that wasn't created yet (spip_ecatalogue_proprietes,  spip_ecatalogue_rubriques_groupes)
        //creer_base();
        $desc = description_table('spip_rubriques');// Update tables

        if (!array_key_exists('id_import', $desc['field']))
        {
          sql_query('Alter table spip_rubriques add column id_import int null ');  
        }
    	ecrire_meta($nom_meta_base_version, $current_version="0.11");
    }
    
    if (version_compare($current_version,"0.12.0","<")) {
		creer_base();
    maj_tables('ecatalogue_prices');
    maj_tables('ecatalogue_prices_group');
    
    
    	// Проверка на существование 1 группы цен
		$id_group = '';
		$r1 = sql_select('id_group','spip_ecatalogue_prices_group',array('id_group = 1','is_active = 1'));
		while ($res = sql_fetch($r1))
		{
			$id_group = $res[1];
		}
		
		if (!$id_group)
		{
			sql_insert('spip_ecatalogue_prices_group','(id_group,titre,is_active)','(1,"Розница",1)');			
		}
		// Запись в таблицу spip_ecatalogue_prices цен из таблицы spip_ecatalogue_products
		$r = sql_select('id_article,price','spip_ecatalogue_products','price<>""');
		while ($res = sql_fetch($r))
		{
			$id_group = 1;
			$id_article = $res['id_article'];
			$price = $res['price'];
			// Проверка что такая цена уже есть в справочнике цен в 1 группе, тогда цену обновляем
			$id_tmp_art = '';
			$r2 = sql_select('id_article','spip_ecatalogue_prices',array('id_article = '.$id_article,'id_group = 1'));
			while ($res = sql_fetch($r2))
			{
				$id_tmp_art = $res['id_article'];
			}
			
			if ($id_tmp_art)
			{
				// Обновляем цену
				sql_update('spip_ecatalogue_prices',array('price'=>$price),array('id_article = '.$id_article,'id_group = 1') );
			}
			else
			{
				// Вставляем цену
				sql_insert('spip_ecatalogue_prices','(id_article,id_group,price)','('.$id_article.',1,'.$price.')');
			}
			
			
		}	
	
    
		ecrire_meta($nom_meta_base_version, $current_version="0.12");
    }
	


    include_spip('base/ecatalogue_db_populate');
}


function ecatalogue_vider_tables($nom_meta_base_version) {
	//spip_log('Plugin ecatalogue correctement dйsinstallй.','ecatalogue');
	effacer_meta($nom_meta_base_version);
}

?>